package com.tgy.kafka.config;

import com.tgy.kafka.constant.Myconstant;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.KafkaAdminClient;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.*;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @ClassName: KafkaConfig
 * @Description: 尝试学习项目中kafka的用法
 * @Auther: tanggy3
 * @Date: 10:38 2022/6/25
 * @version : V1.0
 */
@Configuration//表明这是一个配置类
@ConfigurationProperties(prefix = "tgy.kafka")//绑定配置文件
@EnableKafka
@Setter
@Slf4j
public class KafkaConfig {
    private List<String> bootstrapServers;
    private Map<String, String> producer;
    private Map<String, String> consumer;

    @Bean(name="kafkaTemplate")
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate(tgyProducerFactory());
    }

    @PostConstruct
    public void init() {
        log.info("bean被加载完成后我就执行辣===>{}", Myconstant.SUCCESS);
    }

    @Bean("tgyBatchFactory")
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, Object>> batchFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(tgyConsumerFactory());
        factory.setBatchListener(true);
        return factory;
    }

    /**
     * Producer配置
     */
    @Bean
    public Map<String, Object> tgyProducerConfig() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, producer.get("key-serializer"));
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, producer.get("value-serializer"));
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, producer.get("buffer-memory"));
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, producer.get("batch-size"));
        return props;
    }

    /**
     * Consumer配置
     */
    @Bean
    public Map<String, Object> consumerConfig() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, consumer.get("key-deserializer"));
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, consumer.get("value-deserializer"));
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, consumer.get("auto-offset-reset"));

        return props;
    }

    /**
     * AdminClient配置
     */
    @Bean
    public Map<String, Object> adminConfig() {
        Map<String, Object> props = new HashMap<>();
        props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, consumer.get("key-deserializer"));
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, consumer.get("value-deserializer"));
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, consumer.get("auto-offset-reset"));
        return props;
    }

    @Bean
    public ProducerFactory<String, Object> tgyProducerFactory() {
        return new DefaultKafkaProducerFactory<>(tgyProducerConfig());
    }

    @Bean
    public ConsumerFactory<String, Object> tgyConsumerFactory() {
        return new DefaultKafkaConsumerFactory<String, Object>(consumerConfig());
    }

    @Bean
    public KafkaListenerContainerFactory<?> tgyBatchFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(tgyConsumerFactory());
        factory.setBatchListener(true);
        return factory;
    }

    @Bean("tgyKafkaTemplate")
    public KafkaTemplate<String, Object> tgyKafkaTemplate() {
        return new KafkaTemplate<>(tgyProducerFactory());
    }

    @Bean("tgyKafkaAdminClient")
    public KafkaAdminClient aiopsKafkaAdminClient() {
        return (KafkaAdminClient) KafkaAdminClient.create(adminConfig());
    }
}
